{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "np.tile()的使用"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "import numpy as np\n",
    "def create_anchors_3d_range(\n",
    "    feature_size,             # [1, 200, 176]\n",
    "    anchor_range,             # [0, -40.0, -1.0, 70.4, 40.0, -1.0]\n",
    "    sizes=[1.6, 3.9, 1.56],   # w, l, h\n",
    "    rotations=[0, np.pi / 2],\n",
    "    velocities=None,\n",
    "    dtype=np.float32,\n",
    "):\n",
    "    \"\"\"\n",
    "    Args:\n",
    "        feature_size: list [D, H, W](zyx).\n",
    "        sizes: [N, 3] list of list or array, size of anchors, xyz.\n",
    "        rotations: len(stride) num Reference.\n",
    "        velocities: ref velo along x and y axis.\n",
    "\n",
    "    Returns:\n",
    "        anchors: [*feature_size, num_sizes, num_rots, 7] -> [1, 200, 176, 1, 2, 7]\n",
    "\n",
    "    \"\"\"\n",
    "    # create mesh of anchor centers\n",
    "    anchor_range = np.array(anchor_range, dtype)\n",
    "    stride = (anchor_range[3] - anchor_range[0]) / feature_size[2]    # (70.4 - 0) / 176 = 0.4\n",
    "    z_centers =  np.linspace(anchor_range[2], anchor_range[5], feature_size[0], dtype=dtype)   # [-1, -1]\n",
    "    y_centers = (np.linspace(anchor_range[1], anchor_range[4], feature_size[2], endpoint=False, dtype=dtype,) + stride / 2)  # [-40, 40], move to center\n",
    "    x_centers = (np.linspace(anchor_range[0], anchor_range[3], feature_size[1], endpoint=False, dtype=dtype,) + stride / 2)  # [0, 70.4]\n",
    "    rotations = np.array(rotations, dtype=dtype)                      # [0. 1.57]\n",
    "    rets = np.meshgrid(x_centers, y_centers, z_centers, rotations, indexing=\"ij\")  # rets[i]: (176, 200, 1, 2)\n",
    "    print(\"rets size \", len(rets))\n",
    "    print(\"rets i size \", rets[0].shape)\n",
    "    # unknown operations\n",
    "    sizes = np.reshape(np.array(sizes, dtype=dtype), [-1, 3])   # [1.6, 3.9, 1.56]\n",
    "    tile_shape = [1] * 5\n",
    "    print(\"tile_shape size \", tile_shape)\n",
    "    tile_shape[-2] = int(sizes.shape[0])     # [1, 1, 1, 1, 1]\n",
    "    print(\"tile_shape size \", tile_shape)\n",
    "\n",
    "    for i in range(len(rets)):  # len: 4\n",
    "        rets[i] = np.tile(rets[i][..., np.newaxis, :], tile_shape)\n",
    "        print(\"rets  size \", rets[i].shape)\n",
    "        rets[i] = rets[i][..., np.newaxis]   # rets[i]: (176, 200, 1, 2) -> (176, 200, 1, +1, 2, +1)\n",
    "        print(\"rets  size \", rets[i].shape)\n",
    "    \n",
    "    print(\"rets 2 size \", rets[i].shape)\n",
    "\n",
    "    # prepare anchor size (w, l, h)\n",
    "    if velocities is not None: # False\n",
    "        velocities = np.array(velocities, dtype=dtype).reshape([-1, 2])\n",
    "        combines = np.hstack([sizes, velocities]).reshape([-1, 5])\n",
    "    else:\n",
    "        combines = sizes        # [1, 3]: [1.6, 3.9, 1.56]\n",
    "\n",
    "    combines = np.reshape(combines, [1, 1, 1, -1, 1, combines.shape[-1]])   # [1, 1, 1, 1, 1, 3]\n",
    "    print(\"combines \",combines)\n",
    "    print(\"combines shape \",combines.shape)\n",
    "    tile_size_shape = list(rets[0].shape)  # [176, 200, 1, 1, 2, 1]\n",
    "    print(\"tile size shape \",tile_size_shape)\n",
    "    tile_size_shape[3] = 1                 # [176, 200, 1, 1, 2, 1]\n",
    "    print(\"tile size shape \",tile_size_shape)\n",
    "    combines = np.tile(combines, tile_size_shape)  # (176, 200, 1, 1, 2, 3):  [1.6, 3.9, 1.56]\n",
    "    print(\"combines shape \",combines.shape)\n",
    "\n",
    "    # get anchors <- [x, y, z, inserted: [w, l, h], r]\n",
    "    rets.insert(3, combines)\n",
    "    print(\"rets shape \",rets[4].shape)\n",
    "    ret = np.concatenate(rets, axis=-1)  # (176, 200, 1, 1, 2, 7)\n",
    "    print(\"rets \",ret.shape)\n",
    "\n",
    "    return np.transpose(ret, [2, 1, 0, 3, 4, 5])    #  [*feature_size, num_sizes, num_rots, 7]: (1, 200, 176, 1, 2, 7)\n",
    "\n",
    "create_anchors_3d_range(np.array([1,200,176]),np.array([0, -40.0, -1.0, 70.4, 40.0, -1.0]))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "rets size  4\n",
      "rets i size  (200, 176, 1, 2)\n",
      "tile_shape size  [1, 1, 1, 1, 1]\n",
      "tile_shape size  [1, 1, 1, 1, 1]\n",
      "rets  size  (200, 176, 1, 1, 2)\n",
      "rets  size  (200, 176, 1, 1, 2, 1)\n",
      "rets  size  (200, 176, 1, 1, 2)\n",
      "rets  size  (200, 176, 1, 1, 2, 1)\n",
      "rets  size  (200, 176, 1, 1, 2)\n",
      "rets  size  (200, 176, 1, 1, 2, 1)\n",
      "rets  size  (200, 176, 1, 1, 2)\n",
      "rets  size  (200, 176, 1, 1, 2, 1)\n",
      "rets 2 size  (200, 176, 1, 1, 2, 1)\n",
      "combines  [[[[[[1.6  3.9  1.56]]]]]]\n",
      "combines shape  (1, 1, 1, 1, 1, 3)\n",
      "tile size shape  [200, 176, 1, 1, 2, 1]\n",
      "tile size shape  [200, 176, 1, 1, 2, 1]\n",
      "combines shape  (200, 176, 1, 1, 2, 3)\n",
      "rets shape  (200, 176, 1, 1, 2, 1)\n",
      "rets  (200, 176, 1, 1, 2, 7)\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[[[[[  0.2      , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.2      , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.552    , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.552    , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.904    , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.904    , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         ...,\n",
       "\n",
       "\n",
       "         [[[ 69.544    , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.544    , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 69.895996 , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.895996 , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 70.248    , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 70.248    , -39.8      ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]]],\n",
       "\n",
       "\n",
       "\n",
       "        [[[[  0.2      , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.2      , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.552    , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.552    , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.904    , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.904    , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         ...,\n",
       "\n",
       "\n",
       "         [[[ 69.544    , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.544    , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 69.895996 , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.895996 , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 70.248    , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 70.248    , -39.345455 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]]],\n",
       "\n",
       "\n",
       "\n",
       "        [[[[  0.2      , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.2      , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.552    , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.552    , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.904    , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.904    , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         ...,\n",
       "\n",
       "\n",
       "         [[[ 69.544    , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.544    , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 69.895996 , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.895996 , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 70.248    , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 70.248    , -38.890907 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]]],\n",
       "\n",
       "\n",
       "\n",
       "        ...,\n",
       "\n",
       "\n",
       "\n",
       "        [[[[  0.2      ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.2      ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.552    ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.552    ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.904    ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.904    ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         ...,\n",
       "\n",
       "\n",
       "         [[[ 69.544    ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.544    ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 69.895996 ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.895996 ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 70.248    ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 70.248    ,  38.836365 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]]],\n",
       "\n",
       "\n",
       "\n",
       "        [[[[  0.2      ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.2      ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.552    ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.552    ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.904    ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.904    ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         ...,\n",
       "\n",
       "\n",
       "         [[[ 69.544    ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.544    ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 69.895996 ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.895996 ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 70.248    ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 70.248    ,  39.29091  ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]]],\n",
       "\n",
       "\n",
       "\n",
       "        [[[[  0.2      ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.2      ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.552    ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.552    ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[  0.904    ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [  0.904    ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         ...,\n",
       "\n",
       "\n",
       "         [[[ 69.544    ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.544    ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 69.895996 ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 69.895996 ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]],\n",
       "\n",
       "\n",
       "         [[[ 70.248    ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   0.       ],\n",
       "           [ 70.248    ,  39.745457 ,  -1.       , ...,   3.9      ,\n",
       "              1.56     ,   1.5707964]]]]]], dtype=float32)"
      ]
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.10",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.10 64-bit ('d2l-zh': conda)"
  },
  "interpreter": {
   "hash": "0bedf3452ed48e38a2b75ff9f66ce8ae9ae2e92e8665cb618ee0797d2f46b9e5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}